#import <Foundation/Foundation.h>
#import <objc/runtime.h>

#import "FWIconDotView.h"
#import "FWColorIconView.h"
#import "FWCommon.h"


static CGFloat const pi = 3.14159;


@implementation FWIconDotView

@synthesize icon, inset, beveled;


// ******************************************************************************************
#pragma mark -
#pragma mark Initialisation and object management

- (id)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        inset = 0;
        beveled = NO;
    }
    
    return self;
}

- (id)initWithFrame:(CGRect)frame inset:(CGFloat)ins
{
    if (self = [super initWithFrame:frame]) {
        inset = ins;
    }
    
    return self;
}

- (void)dealloc {
    [icon release];
    
    [super dealloc];
}


// ******************************************************************************************
#pragma mark -
#pragma mark Attribute access

- (void)setIcon:(NSString*)newIcon
{
    if (icon == newIcon)
        return;
    
    if (newIcon && icon && [newIcon isEqual:icon])
        return;
    
    [newIcon retain];
    [icon release];
    icon = newIcon;
    
    [self setNeedsDisplay];
}


// ******************************************************************************************
#pragma mark -
#pragma mark Drawing

- (void)drawRect:(CGRect)rect
{
    [super drawRect:rect withGloss:NO];

    if (icon) {
        UIImage* image = [UIImage imageWithContentsOfFile:icon];
        if (image != nil) {
            CGRect iconRect = CGRectInset(rect, inset, inset);

            CGContextRef context = UIGraphicsGetCurrentContext();
            CGContextSaveGState(context);

            UIImage* bevelImage = nil;
            if (beveled) {
                FWColorIconView* bevel = [[[FWColorIconView alloc]
                                           initWithFrame:CGRectMake(0, 0, iconRect.size.width, iconRect.size.height)] autorelease];
                bevel.icon = icon;
                bevel.color = [UIColor whiteColor];
                
                UIGraphicsBeginImageContext(bevel.frame.size);// a CGSize that has the size you want
                [bevel drawRect:CGRectMake(0,0,bevel.frame.size.width, bevel.frame.size.height)];
                bevelImage = UIGraphicsGetImageFromCurrentImageContext();
                UIGraphicsEndImageContext();
            }

            CGContextBeginPath(context);
            CGContextAddArc(context,
                            iconRect.origin.x + iconRect.size.width / 2,
                            iconRect.origin.y + iconRect.size.height / 2,
                            iconRect.size.height / 2, 0,
                            2 * pi, 0);
            CGContextClosePath(context);
            CGContextClip (context);

            if (bevelImage) {
                CGRect bevelRect = iconRect;
                ++bevelRect.origin.x;
                ++bevelRect.origin.y;
                [bevelImage drawInRect:bevelRect];
            }
            
            [image drawInRect:iconRect];
                        
            CGContextRestoreGState(context);
        }
        else
            XLog(@"LI:PICommon: Cannot find image %@", icon);
    }
    
    [super drawGloss:rect];
}

@end
